
package com.xiaoma.myaudience.biz.view;

import android.content.Context;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xiaoma.myaudience.R;

/**
 * 圆点样式的指示器.
 * 
 */
public class DotIndicator extends Indicator {
    private final static String TAG = "SlideDotIndicator";

    /**
     * 圆点中是否显示数字.
     */
    private boolean mIsShowNum = false;

    public DotIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DotIndicator(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
    }

    public void onSetTotalItems() {
        detachAllViewsFromParent();

        final int totalItems = getTotalItems();
        if (totalItems <= 0) {
            return;
        }

        final LayoutInflater inflater = LayoutInflater.from(getContext());

        for (int i = 0; i < totalItems; i++) {
            DotIndicatorItem dot = (DotIndicatorItem)inflater.inflate(R.layout.dot_indicator_item, null);
            TransitionDrawable td = (TransitionDrawable) dot.getDrawable();
            td.setCrossFadeEnabled(true);
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            addView(dot, p);
        }
    }

    public void onSetCurrentItem() {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            DotIndicatorItem item = (DotIndicatorItem) getChildAt(i);
            if (mIsShowNum) {
                item.resetNum();
            }
            TransitionDrawable drawable = (TransitionDrawable) item.getDrawable();
            drawable.resetTransition();
        }

        final int currentItem = getCurrentItem();
        if (currentItem < 0 || currentItem >= count) {
            return;
        }

        DotIndicatorItem item = (DotIndicatorItem) getChildAt(currentItem);
        if (mIsShowNum) {
            item.setNum(currentItem);
        }
        TransitionDrawable drawable = (TransitionDrawable) item.getDrawable();
        drawable.startTransition(50);
    }


}
